package sjtu.ist.warehouse4ws.tool;

import java.io.File;
import java.util.ArrayList;

import org.junit.Test;

import sjtu.ist.warehouse4ws.model.ModelFilePojo;

public class FileIO {
	ArrayList str1 = new ArrayList();

	// 获取文件列表
	public ArrayList<String> getFilepath(String filepath) {
		ArrayList str = new ArrayList();
		File file = new File(filepath);
		if ((!file.exists()) || (!file.isDirectory())) {
			str = null;
		}
		File[] files = file.listFiles();
		for (int i = 0; i < files.length; i++) {
			if (files[i].isFile()) { // 判断他是文件
				String path = filepath + "/" + files[i].getName();
				str.add(path);
			}
		}

		return str;

	}

	public ArrayList<String> getAllDocument(String docpath) {

		File file = new File(docpath);
		if ((!file.exists()) || (!file.isDirectory())) {
			str1 = null;
		}
		File[] files = file.listFiles();
		for (int i = 0; i < files.length; i++) {
			if (!files[i].isFile()) { // 判断它是文件夹
				String path = docpath + "/" + files[i].getName();
				str1.add(path);
				getAllDocument(path);
			}
		}
		return str1;
	}

	public String getProjectFile(String docpath) {
		String str = null;
		File file = new File(docpath);
		if ((!file.exists()) || (!file.isDirectory())) {
			str = null;
		}
		File[] files = file.listFiles();
		for (int i = 0; i < files.length; i++) {
			if (files[i].isFile()) { // 判断他是文件
				str = docpath + "/" + files[i].getName();

			}
		}
		return str;
	}

	public ModelFilePojo getAllModelData(String docpath) {
		ModelFilePojo data = new ModelFilePojo();
		String projectFile = getProjectFile(docpath);
		data.setProjectFile(projectFile);
		ArrayList<String> doclist = getAllDocument(docpath); // 获取所有文件夹路径

		for (int i = 0; i < doclist.size(); i++) {// 判断他是树中哪个节点的数据
			if (doclist.get(i).endsWith("业务对象视图")) {
				ArrayList<String> domainView = getFilepath(doclist.get(i));
				data.setDomainView(domainView);
			}
			if (doclist.get(i).endsWith("功能视图")) {
				ArrayList<String> functionView = getFilepath(doclist.get(i));
				data.setFunctionView(functionView);
			}
			if (doclist.get(i).endsWith("控制视图")) {
				ArrayList<String> controlView = getFilepath(doclist.get(i));
				data.setControlView(controlView);
			}
			if (doclist.get(i).endsWith("数据视图")) {
				ArrayList<String> dataView = getFilepath(doclist.get(i));
				data.setDataView(dataView);
			}
			if (doclist.get(i).endsWith("组织视图")) {
				ArrayList<String> orgView = getFilepath(doclist.get(i));
				data.setOrgView(orgView);
			}

			if (doclist.get(i).endsWith("models")) {
				ArrayList<String> models = getFilepath(doclist.get(i));
				data.setModels(models);
			}
			if (doclist.get(i).endsWith("领域模型")) {
				ArrayList<String> domainmodel = getFilepath(doclist.get(i));
				data.setDomainmodel(domainmodel);
				// System.out.println(doclist.get(i));
			}
			if (doclist.get(i).endsWith("functions")) {
				ArrayList<String> functions = getFilepath(doclist.get(i));
				data.setFunctions(functions);
			}
			if (doclist.get(i).endsWith("processes")) {
				ArrayList<String> processes = getFilepath(doclist.get(i));
				data.setProcesses(processes);
			}
			if (doclist.get(i).endsWith("功能点")) {
				ArrayList<String> functionPoint = getFilepath(doclist.get(i));
				data.setFunctionPoint(functionPoint);
			}
			if (doclist.get(i).endsWith("流程")) {
				ArrayList<String> process = getFilepath(doclist.get(i));
				data.setProcess(process);
			}
			if (doclist.get(i).endsWith("bpel")) {
				ArrayList<String> bpel = getFilepath(doclist.get(i));
				data.setBpel(bpel);
			}
			if (doclist.get(i).endsWith("epc")) {
				ArrayList<String> epc = getFilepath(doclist.get(i));
				data.setEpc(epc);
			}
			if (doclist.get(i).endsWith("templates")) {
				ArrayList<String> templates = getFilepath(doclist.get(i));
				data.setTemplates(templates);
			}
			if (doclist.get(i).endsWith("perms")) {
				ArrayList<String> perms = getFilepath(doclist.get(i));
				data.setPerms(perms);
			}
			if (doclist.get(i).endsWith("roles")) {
				ArrayList<String> roles = getFilepath(doclist.get(i));
				data.setRoles(roles);
			}

		}

		return data;

	}

	@Test
	public void test() {
		String filepath = "E:/Users/chenqidou/Desktop/demo2/功能视图/functions";
		String docpath = "E:/Users/chenqidou/Desktop/demo2";
		// getFilepath(filepath);
		// ArrayList<String> str=getAllDocument(docpath);
		// for(int j=0;j<str.size();j++){
		//
		// System.out.println(str.get(j));
		// }
		// System.out.print(getProjectFile(docpath));
		ModelFilePojo data = getAllModelData(docpath);
		if (data.getFunctionView().isEmpty()) {
			System.out.print("--------------------is empty!");
		}
	}

}
